﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace Calc
{
    public partial class frmConcreteCalculator : Form
    {
        public frmConcreteCalculator()
        {
            InitializeComponent();
        }
        int count = 0;
        int MeasurementChildID = 0;
        CalculatorEntities objCalculatorEntities = new CalculatorEntities();
        ProjectDB objProjectDB = new ProjectDB();
        ManageItems objManageItems = new ManageItems();
        decimal cubicinches = 46656;
        decimal pi = 3.14159265M;

        private void frmConcreteCalculator_Load(object sender, EventArgs e)
        {
            GetConcreteCalculator();
        }

        private void GetConcreteCalculator()
        {
            objCalculatorEntities.Action = 0;
            DataSet dsConcrete = new DataSet();
            dsConcrete = objProjectDB.ManageConcreteCalculator(objCalculatorEntities);

            if (dsConcrete.Tables[0].Rows.Count > 0)
            {
                count = 1;
                MeasurementChildID=Convert.ToInt16(dsConcrete.Tables[0].Rows[0]["MeasurementChildID"].ToString());
                txtSquareInches.Text = dsConcrete.Tables[0].Rows[0]["SquareInches"].ToString();
                txtCubicYards.Text = dsConcrete.Tables[0].Rows[0]["CubicYards"].ToString();
                txtWidth.Text = dsConcrete.Tables[0].Rows[0]["Width"].ToString();
                txtHeight1.Text=dsConcrete.Tables[0].Rows[0]["Height1"].ToString();
                txtHeight2.Text = dsConcrete.Tables[0].Rows[0]["Height2"].ToString();
                txtHeight3.Text = dsConcrete.Tables[0].Rows[0]["Height3"].ToString();
                txtHeight4.Text = dsConcrete.Tables[0].Rows[0]["Height4"].ToString();
                txtHeight5.Text = dsConcrete.Tables[0].Rows[0]["Height5"].ToString();
                txtHeight6.Text = dsConcrete.Tables[0].Rows[0]["Height6"].ToString();
                txtHeight7.Text = dsConcrete.Tables[0].Rows[0]["Height7"].ToString();
                txtHeight8.Text = dsConcrete.Tables[0].Rows[0]["Height8"].ToString();
                txtHeight9.Text = dsConcrete.Tables[0].Rows[0]["Height9"].ToString();
                txtHeight10.Text = dsConcrete.Tables[0].Rows[0]["Height10"].ToString();
                txtHeight11.Text = dsConcrete.Tables[0].Rows[0]["Height11"].ToString();
                txtHeight12.Text = dsConcrete.Tables[0].Rows[0]["Height12"].ToString();
                txtCubicYards1.Text = dsConcrete.Tables[0].Rows[0]["CubicYards1"].ToString();
                txtDiameter.Text = dsConcrete.Tables[0].Rows[0]["Diameter"].ToString();
                txtCubicYards2.Text = dsConcrete.Tables[0].Rows[0]["CubicYards2"].ToString();
                txtLength1.Text = dsConcrete.Tables[0].Rows[0]["Length1"].ToString();
                txtWidth1.Text = dsConcrete.Tables[0].Rows[0]["Width1"].ToString();
                txtHeight.Text = dsConcrete.Tables[0].Rows[0]["Height"].ToString();
                txtCubicYards3.Text = dsConcrete.Tables[0].Rows[0]["CubicYards3"].ToString();
                txtNumberofInches.Text = dsConcrete.Tables[0].Rows[0]["NumberofInches"].ToString();
                txtCubicYards4.Text = dsConcrete.Tables[0].Rows[0]["CubicYards4"].ToString();
            }
        }

        private void txtSquareInches_Leave(object sender, EventArgs e)
        {
            //float cubicyards = Convert.ToInt16(txtSquareInches.Text) * 12;
            txtCubicYards.Text =calucateCubicYards(txtSquareInches.Text);
        }

        private void CalculateLinearIrregular(object sender, EventArgs e)
        {
            string width = "";
            int count = 0;
            decimal total=0;
            if (txtWidth.Text != "")
                width = txtWidth.Text;
            if (txtHeight1.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight1.Text);
            }
            if (txtHeight2.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight2.Text);
            }
            if (txtHeight3.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight3.Text);
            }
            if (txtHeight4.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight4.Text);
            }
            if (txtHeight5.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight5.Text);
            }
            if (txtHeight6.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight6.Text);
            }
            if (txtHeight7.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight7.Text);
            }
            if (txtHeight8.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight8.Text);
            }
            if (txtHeight9.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight9.Text);
            }
            if (txtHeight10.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight10.Text);
            }
            if (txtHeight11.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight11.Text);
            }
            if (txtHeight12.Text != "")
            {
                count = count + 1;
                total = total + Convert.ToDecimal(txtHeight12.Text);
            }

            if (count == 0 || width == "")
            {
                txtCubicYards1.Text = "?";
            }
            else
            {
                decimal avg = total / count;
                decimal avg1 = avg * 12;
                txtCubicYards1.Text = calucateCubicYards(avg1.ToString());

            }
        }

        private string calucateCubicYards(string CubicYards)
        {
            decimal cubicyards1 =Convert.ToDecimal(CubicYards) * 12;
            return Convert.ToString(decimal.Round((cubicinches / cubicyards1),3));
        }

        private void txtDiameter_Leave(object sender, EventArgs e)
        {
            decimal avediameter = Convert.ToDecimal(txtDiameter.Text) / 2;
            decimal muldiamenter = avediameter * avediameter * pi * 12;
            decimal ave = cubicinches / muldiamenter;
            decimal total1 =decimal.Round(ave,3);
            txtCubicYards2.Text = total1.ToString();
        }
        private void CalculateCount(object sender, EventArgs e)
        { 
            if (txtLength1.Text!="" && txtWidth1.Text!="" && txtHeight.Text!="")
            {
                decimal ave = Convert.ToDecimal(txtLength1.Text) * Convert.ToDecimal(txtWidth1.Text) * Convert.ToDecimal(txtHeight.Text);
                txtCubicYards3.Text = Convert.ToString(decimal.Round((cubicinches / ave),3));
            }
            else
            {
                txtCubicYards3.Text="?";
            }
        }

        private void txtNumberofInches_Leave(object sender, EventArgs e)
        {
            if (txtNumberofInches.Text != "" && Convert.ToInt16(txtNumberofInches.Text) != 0)
            {
                int thickness = 324;
                decimal coverage = thickness / Convert.ToInt16(txtNumberofInches.Text);
                coverage = decimal.Round(coverage, 3);
                txtCubicYards4.Text = coverage.ToString();
            }
        }

        private void CancelClick(object sender, EventArgs e)
        {
            this.Dispose();
        }

        

        private void SaveConcreteDetails()
        { 
            if (count == 0)
                objCalculatorEntities.Action = 1;
            else
                objCalculatorEntities.Action = 2;
            objCalculatorEntities.MeasurementChildID = ProjectDB.ID;
            objCalculatorEntities.SquareInches = txtSquareInches.Text;
            objCalculatorEntities.CubicYards = txtCubicYards.Text;
            objCalculatorEntities.Width = txtWidth.Text;
            objCalculatorEntities.Height1 = txtHeight1.Text;
            objCalculatorEntities.Height2 = txtHeight2.Text;
            objCalculatorEntities.Height3 = txtHeight3.Text;
            objCalculatorEntities.Height4 = txtHeight4.Text;
            objCalculatorEntities.Height5 = txtHeight5.Text;
            objCalculatorEntities.Height6 = txtHeight6.Text;
            objCalculatorEntities.Height7 = txtHeight7.Text;
            objCalculatorEntities.Height8 = txtHeight8.Text;
            objCalculatorEntities.Height9 = txtHeight9.Text;
            objCalculatorEntities.Height10 = txtHeight10.Text;
            objCalculatorEntities.Height11 = txtHeight11.Text;
            objCalculatorEntities.Height12 = txtHeight12.Text;
            objCalculatorEntities.CubicYards1 = txtCubicYards1.Text;
            objCalculatorEntities.Diameter = txtDiameter.Text;
            objCalculatorEntities.CubicYards2 = txtCubicYards2.Text;
            objCalculatorEntities.Length1 = txtLength1.Text;
            objCalculatorEntities.Width1 = txtWidth1.Text;
            objCalculatorEntities.Height = txtHeight.Text;
            objCalculatorEntities.CubicYards3 = txtCubicYards3.Text;
            objCalculatorEntities.NumberofInches = txtNumberofInches.Text;
            objCalculatorEntities.CubicYards4 = txtCubicYards4.Text;
            objProjectDB.ManageConcreteCalculator(objCalculatorEntities);
        }

        private void btnAccept1_Click(object sender, EventArgs e)
        {
            SaveConcreteDetails();

            SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
            SqlCeCommand cmd = new SqlCeCommand("Update MeasurementChild set Coverage='" + txtCubicYards.Text + "' where ID=" + ProjectDB.ID, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            
            frmMainTab objtab = new frmMainTab();
            ProjectDB.Coverage = txtCubicYards.Text;
            objtab.txtCoverage.Refresh();
            objtab.BindDatatoWorkSheet();
            objtab.Activate();
            this.Dispose();
        }

        private void btnAccept2_Click(object sender, EventArgs e)
        {
            SaveConcreteDetails();

            SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
            SqlCeCommand cmd = new SqlCeCommand("Update MeasurementChild set Coverage='" + txtCubicYards1.Text + "' where ID=" + ProjectDB.ID, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            frmMainTab objtab = new frmMainTab();
            objtab.txtCoverage.Text = txtCubicYards1.Text;
            this.Dispose();
            objtab.BindDatatoWorkSheet();
            objtab.Activate();
        }

        private void btnAccept3_Click(object sender, EventArgs e)
        {
            SaveConcreteDetails();

            SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
            SqlCeCommand cmd = new SqlCeCommand("Update MeasurementChild set Coverage='" + txtCubicYards2.Text + "' where ID=" + ProjectDB.ID, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            frmMainTab objtab = new frmMainTab();
            objtab.txtCoverage.Text = txtCubicYards2.Text;
            this.Dispose();
            objtab.BindDatatoWorkSheet();
            objtab.Activate();
        }

        private void btnAccept4_Click(object sender, EventArgs e)
        {
            SaveConcreteDetails();

            SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
            SqlCeCommand cmd = new SqlCeCommand("Update MeasurementChild set Coverage='" + txtCubicYards3.Text + "' where ID=" + ProjectDB.ID, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            frmMainTab objtab = new frmMainTab();
            objtab.txtCoverage.Text = txtCubicYards3.Text;
            this.Dispose();
            objtab.BindDatatoWorkSheet();
            objtab.Activate();
        }

        private void btnAccept5_Click(object sender, EventArgs e)
        {
            SaveConcreteDetails();

            SqlCeConnection cn = new SqlCeConnection(objManageItems.GetConnectionString());
            SqlCeCommand cmd = new SqlCeCommand("Update MeasurementChild set Coverage='" + txtCubicYards4.Text + "' where ID=" + ProjectDB.ID, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            frmMainTab objtab = new frmMainTab();
            objtab.txtCoverage.Text = txtCubicYards4.Text;
            this.Dispose();
            objtab.BindDatatoWorkSheet();
            objtab.Activate();
        }

       
    }
}
